package m.server.web.res;

import m.system.util.StringUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class ContentDisposition {
    private ContentDisposition(){}
    public static ContentDisposition inline(){
        ContentDisposition contentDisposition=new ContentDisposition();
        contentDisposition.disposition="inline";
        return contentDisposition;
    }
    public static ContentDisposition attachment(String fileName){
        ContentDisposition contentDisposition=new ContentDisposition();
        contentDisposition.disposition="attachment";
        contentDisposition.fileName=fileName;
        return contentDisposition;
    }
    private String disposition;
    private String fileName;

    @Override
    public String toString() {
        try {
            return disposition+(StringUtil.notSpace(fileName)?("; filename=\""+ URLEncoder.encode(fileName,"UTF-8")+"\""):"");
        } catch (UnsupportedEncodingException e) {
            return disposition;
        }
    }
}
